package com.lucky.common.controller;


import com.lucky.common.cache.AreaCache;
import com.lucky.common.cache.DictCache;
import com.lucky.common.pojo.vo.AreaVO;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/common")
public class CommonController  {
	
	@Resource
	AreaCache areaCache;
	
	@Resource
	DictCache dictCache;
	
	@ResponseBody
	@RequestMapping("/areaCode")
	public List<AreaVO> index() {
		return areaCache.treeList;
	}
	
	@ResponseBody
	@GetMapping("/area")
	public List<AreaVO> area() {
		return areaCache.treeList;
	}

	@ResponseBody
	@GetMapping("/province")
	public List<AreaVO> province() {
        return areaCache.provinceList;
	}

	@ResponseBody
	@GetMapping("/city/{provincecode}")
	public List<AreaVO> city(@PathVariable(value="provincecode") String provincecode) {
//        return areaCache.areaList.stream().filter(a->a.getId().equals(provincecode)).findFirst().orElse(new AreaVO()).getChildren();
//		return areaCache.areaTree.get(provincecode).stream().map(s-> new AreaVO(s.getId(),s.getText())).collect(Collectors.toList());
        return areaCache.treeMap.get(provincecode);
	}

	@ResponseBody
	@GetMapping("/county/{citycode}")
	public List<AreaVO> county(@PathVariable(value="citycode") String citycode) {
		return areaCache.treeMap.get(citycode);
	}
	
}
